Skip to content

feat: async client, SignalR user hub, live control and typed errors - #11

Merged
hhvrc merged 9 commits into
masterfrom
feat/async-and-realtime
Dec 4, 2026
Merged

hhvrc merged 9 commits into
masterfrom
feat/async-and-realtime

Conversation

@hhvrc

@hhvrc hhvrc commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Brings the Python SDK up to the C# SDK's feature set (and covers the rest of the user hub), and fixes a few REST methods that could not work against the current backend.

Added

  • Async REST client: AsyncOpenShockClient / AsyncOpenShockHubClient, same resources and methods as the sync clients. The sync client is now generated from the async code by scripts/unasync.py; CI fails if the two drift.
  • SignalR user hub: openshock.realtime.UserHubClient
    • Typed events: welcome, hub status, logs, hub updates, and the five OTA events
    • Calls: control, set_captive_portal, emergency_stop, ota_install, reboot
    • Reconnects with the C# SDK's delays (0s, 2s, 10s, 30s); a rejected or revoked token ends the session instead of retrying forever
  • Live control: openshock.realtime.LiveControlClient
    • Looks up the gateway on every connection attempt and waits in the background while the hub is offline
    • intake_frame() feeds a tick loop at the gateway-announced rate; handles ping/pong, latency, and rejection responses
  • Typed errors: problem details are parsed into specific exceptions (ShockerPaused, ShockerNoPermission, ShockerNotFoundOrNoAccess, HubOffline, HubNotFound, TokenPermissionMissing, ApiTokenPaused, OpenShockRateLimited)
  • Real enums (ControlType, ShockerModelType, PermissionType, ...) that still compare equal to their wire strings and do not raise on values the SDK does not know yet
  • User-Agent with SDK version, Python, OS and the caller's app_name / app_version
  • client.devices.gateway() for GET /2/devices/{id}/lcg
  • Four standalone examples under examples/, and a rewritten README

Realtime is asyncio-only and behind an optional extra: pip install "openshock[realtime]" (adds websockets). The SignalR client is a small in-house implementation of the JSON protocol with negotiation skipped, sharing its reconnecting transport with live control.

Fixed

  • devices.create() parsed the response as an object; the backend returns a bare ID
  • devices.pair_code() did the same; the backend returns the code as a string
  • account.create_token() read createdOn where the backend sends createdAt, and dropped the token secret

Breaking changes from 0.1.0b1

  • shockers.list_own(), list_shared(), get() and all_logs() return typed models instead of dicts
  • Methods that returned an ID as str now return UUID
  • shockers.control() returns None; the pause methods return the new state
  • OpenShockAuthError and OpenShockNotFound are now subclasses of OpenShockApiError, so except order matters
  • PairCode is removed
  • ShareInfo.shared_with is a BasicUserInfo rather than a dict

Version is bumped to 0.2.0b1.

Notes for review

  • Naming: the SignalR client is UserHubClient, because OpenShockHubClient already means the device-token REST client in this SDK. The C# SDK uses that name for the SignalR client.
  • Hub commands wait for the server's completion message rather than fire-and-forget as in the C# SDK. That surfaces hub errors at the cost of a round trip.
  • SignalR carries enums as integers (the backend registers no string enum converter for the hub protocol), so the hub client sends and parses them that way; REST and live control use strings.
  • Production answers a refused WebSocket upgrade with a response the websockets client cannot parse. The transport repeats such a failure as a plain GET so a bad token is reported as OpenShockAuthError rather than a network error.
  • openshock/_sync/ is generated. Review openshock/_async/ instead.

Testing

  • 144 tests, 94% coverage: every REST method in sync and async form against a mocked transport, and the hub and live control clients against in-process fake servers (handshake, events, invocations, reconnects, auth failures, tick loop, ping/pong, offline hub)
  • Passes on Python 3.13 and 3.10, and with websockets 13.0 and latest; ruff and strict mypy are clean
  • Against production: only the public stats endpoint and the invalid-token paths (REST, hub, live control) were exercised

Not yet verified against production with a real token: receiving hub events and sending live control frames. examples/realtime_events and examples/live_control are the quickest way to check both.

Summary by CodeRabbit

  • New Features
    • Added synchronous and asynchronous clients for REST APIs, hub/device operations, and realtime connections.
    • Added live control, user-hub events and commands, typed API errors, and richer models and enums.
    • Added examples for REST quick starts, async commands, live control, and realtime events.
  • Documentation
    • Expanded setup and usage guidance, including optional realtime installation and self-hosted configuration.
  • Bug Fixes
    • Improved API response parsing, error details, and handling of optional or inconsistent data.
  • Chores
    • Added CI checks to verify generated synchronous clients stay current.

hhvrc added 6 commits December 4, 2026 14:33
Bring the SDK up to the C# SDK's feature set and beyond.

- Add AsyncOpenShockClient / AsyncOpenShockHubClient. The sync client is now
  generated from the async code by scripts/unasync.py (checked in CI).
- Add openshock.realtime (optional 'realtime' extra, asyncio only):
  UserHubClient for the SignalR user hub and LiveControlClient for the live
  control gateway, sharing a reconnecting WebSocket transport.
- Parse problem details into typed exceptions (ShockerPaused, HubOffline,
  TokenPermissionMissing, ...). OpenShockAuthError and OpenShockNotFound are
  now subclasses of OpenShockApiError.
- Replace string aliases with real enums that still compare equal to their
  wire strings and tolerate unknown backend values.
- Send a descriptive User-Agent including app_name / app_version.
- Add devices.gateway() for GET /2/devices/{id}/lcg.
- Fix devices.create(), devices.pair_code() and account.create_token(), which
  parsed responses the backend does not send.
- Return typed models from shockers.list_own/list_shared/get/all_logs and
  UUIDs from methods that create things. Remove the PairCode model.
- Add tests for every REST method, the hub and live control (against
  in-process fake servers), four standalone examples, and a rewritten README.
- Bump version to 0.2.0b1.
Replace the hand-written REST resources and models with a client generated
by openapi-python-client from a committed snapshot of the backend's OpenAPI
document (scripts/generate.py, checked in CI).

BREAKING: REST calls now go through openshock.rest.api.*, Python 3.10 is
no longer supported, and httpx is replaced by httpx2.
- refuse concurrent start(), let stop() abort a connection attempt
- bind each event dispatcher to its own queue; bound dispatcher shutdown
- honour allowReconnect for a close sent with the SignalR handshake
- reject non-finite hold_for in live control
- redact credentials in URLs put into exception messages
- tolerate a malformed problem status; ASCII-only User-Agent fields
- pin example requirements; make pure-Python install skip pip's cache
- replace fixed sleeps in live control tests with observed ticks
…ient

The SDK authenticates with API tokens or a hub's device token. Operations
tagged Admin and operations that only accept the browser session cookie are
removed from the OpenAPI document before generation, together with the models
only they used (134 -> 75 operations).
@hhvrc
hhvrc requested review from a team and LucHeart as code owners December 4, 2026 15:57
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Too many files!

This PR contains 235 files, which is 135 over the limit of 100.

To get a review, reduce the PR to 100 files or fewer by splitting it into smaller PRs or changing its base branch.

Upgrade to a paid plan to raise the limit.

This organization's CodeRabbit subscription is cancelled. Resubscribe in Billing to continue reviews beyond the free-tier limit.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c488610b-ee46-4af7-823e-c5d456f7c2bc
Commits

Reviewing files that changed from the base of the PR and between 020c5e2 and 1bda8df.

Files selected for processing: 235

  • .github/workflows/ci.yml
  • README.md
  • examples/README.md
  • examples/async_control/README.md
  • examples/async_control/main.py
  • examples/async_control/requirements.txt
  • examples/live_control/README.md
  • examples/live_control/main.py
  • examples/live_control/requirements.txt
  • examples/realtime_events/README.md
  • examples/realtime_events/main.py
  • examples/realtime_events/requirements.txt
  • examples/rest_quickstart/README.md
  • examples/rest_quickstart/main.py
  • examples/rest_quickstart/requirements.txt
  • openapi/generator-config.yaml
  • openshock/__init__.py
  • openshock/_core.py
  • openshock/_version.py
  • openshock/auth.py
  • openshock/client.py
  • openshock/exceptions.py
  • openshock/models/__init__.py
  • openshock/models/account.py
  • openshock/models/common.py
  • openshock/models/device.py
  • openshock/models/meta.py
  • openshock/models/session.py
  • openshock/models/share.py
  • openshock/models/shocker.py
  • openshock/realtime/__init__.py
  • openshock/realtime/_events.py
  • openshock/realtime/_signalr.py
  • openshock/realtime/_transport.py
  • openshock/realtime/_wire.py
  • openshock/realtime/live_control.py
  • openshock/realtime/models.py
  • openshock/realtime/user_hub.py
  • openshock/resources/__init__.py
  • openshock/resources/account.py
  • openshock/resources/devices.py
  • openshock/resources/hub.py
  • openshock/resources/meta.py
  • openshock/resources/sessions.py
  • openshock/resources/shares.py
  • openshock/resources/shockers.py
  • openshock/resources/user_shares.py
  • openshock/rest/__init__.py
  • openshock/rest/api/__init__.py
  • openshock/rest/api/account/__init__.py
  • openshock/rest/api/account/authenticated_account_get_self.py
  • openshock/rest/api/api_tokens/__init__.py
  • openshock/rest/api/api_tokens/token_delete_delete_token.py
  • openshock/rest/api/api_tokens/tokens_self_get_self_token_v2.py
  • openshock/rest/api/hub_endpoints/__init__.py
  • openshock/rest/api/hub_endpoints/device_get_live_control_gateway_v2.py
  • openshock/rest/api/hub_endpoints/device_get_self.py
  • openshock/rest/api/hub_endpoints/device_pair.py
  • openshock/rest/api/hub_management/__init__.py
  • openshock/rest/api/hub_management/devices_create_device_v2.py
  • openshock/rest/api/hub_management/devices_edit_device.py
  • openshock/rest/api/hub_management/devices_get_device_by_id.py
  • openshock/rest/api/hub_management/devices_get_live_control_gateway_info_v2.py
  • openshock/rest/api/hub_management/devices_get_pair_code.py
  • openshock/rest/api/hub_management/devices_get_shockers.py
  • openshock/rest/api/hub_management/devices_list_devices.py
  • openshock/rest/api/hub_management/devices_regenerate_device_token.py
  • openshock/rest/api/hub_management/devices_remove_device.py
  • openshock/rest/api/integrations/__init__.py
  • openshock/rest/api/integrations/integrations_get_control_stat_changes.py
  • openshock/rest/api/meta/__init__.py
  • openshock/rest/api/meta/public_get_online_devices_statistics.py
  • openshock/rest/api/meta/version_get_backend_info.py
  • openshock/rest/api/public_shocker_shares/__init__.py
  • openshock/rest/api/public_shocker_shares/public_get_public_share.py
  • openshock/rest/api/public_shocker_shares/public_get_public_share_by_slug.py
  • openshock/rest/api/public_shocker_shares/public_unlock_public_share.py
  • openshock/rest/api/public_shocker_shares/share_links_add_shocker.py
  • openshock/rest/api/public_shocker_shares/share_links_create_public_share.py
  • openshock/rest/api/public_shocker_shares/share_links_delete_public_share.py
  • openshock/rest/api/public_shocker_shares/share_links_edit_public_share.py
  • openshock/rest/api/public_shocker_shares/share_links_edit_shocker.py
  • openshock/rest/api/public_shocker_shares/share_links_list.py
  • openshock/rest/api/public_shocker_shares/share_links_pause_shocker.py
  • openshock/rest/api/public_shocker_shares/share_links_remove_shocker.py
  • openshock/rest/api/public_shocker_shares/share_links_set_public_share_password.py
  • openshock/rest/api/shockers/__init__.py
  • openshock/rest/api/shockers/shocker_edit_shocker.py
  • openshock/rest/api/shockers/shocker_get_all_shocker_logs.py
  • openshock/rest/api/shockers/shocker_get_shocker_by_id.py
  • openshock/rest/api/shockers/shocker_get_shocker_logs.py
  • openshock/rest/api/shockers/shocker_get_user_shares.py
  • openshock/rest/api/shockers/shocker_list_shared_shockers.py
  • openshock/rest/api/shockers/shocker_list_shockers.py
  • openshock/rest/api/shockers/shocker_pause_shocker.py
  • openshock/rest/api/shockers/shocker_register_shocker.py
  • openshock/rest/api/shockers/shocker_remove_shocker.py
  • openshock/rest/api/shockers/shocker_send_control.py
  • openshock/rest/api/shockers/shocker_set_shocker_permissions.py
  • openshock/rest/api/shockers/shocker_shocker_share_code_create.py
  • openshock/rest/api/shockers/shocker_shocker_share_code_list.py
  • openshock/rest/api/shockers/shocker_shocker_share_code_pause.py
  • openshock/rest/api/shockers/shocker_shocker_share_code_update.py
  • openshock/rest/api/shockers/shocker_shocker_share_remove.py
  • openshock/rest/api/user_shocker_shares/__init__.py
  • openshock/rest/api/user_shocker_shares/user_shares_bulk_pause_user_share_shockers.py
  • openshock/rest/api/user_shocker_shares/user_shares_bulk_remove_user_share_shockers.py
  • openshock/rest/api/user_shocker_shares/user_shares_bulk_user_share_shockers_update.py
  • openshock/rest/api/user_shocker_shares/user_shares_create_share_invite.py
  • openshock/rest/api/user_shocker_shares/user_shares_delete_outgoing_invite.py
  • openshock/rest/api/user_shocker_shares/user_shares_deny_incoming_invite.py
  • openshock/rest/api/user_shocker_shares/user_shares_get_incoming_invites_list.py
  • openshock/rest/api/user_shocker_shares/user_shares_get_outgoing_invites_list.py
  • openshock/rest/api/user_shocker_shares/user_shares_get_shares_by_users.py
  • openshock/rest/api/user_shocker_shares/user_shares_redeem_invite.py
  • openshock/rest/api/users/__init__.py
  • openshock/rest/api/users/public_get_user_profile.py
  • openshock/rest/api/users/users_get_by_name.py
  • openshock/rest/api/users/users_get_self.py
  • openshock/rest/api/users/users_get_self_control_stats.py
  • openshock/rest/client.py
  • openshock/rest/errors.py
  • openshock/rest/models/__init__.py
  • openshock/rest/models/backend_info_response.py
  • openshock/rest/models/basic_user_info.py
  • openshock/rest/models/bulk_user_share_shockers_update_request.py
  • openshock/rest/models/control.py
  • openshock/rest/models/control_limit_mode.py
  • openshock/rest/models/control_log_sender_light.py
  • openshock/rest/models/control_request.py
  • openshock/rest/models/control_stat_changes_request.py
  • openshock/rest/models/control_stat_changes_response.py
  • openshock/rest/models/control_stat_totals.py
  • openshock/rest/models/control_stats_response.py
  • openshock/rest/models/control_type.py
  • openshock/rest/models/create_share_request.py
  • openshock/rest/models/daily_control_stats.py
  • openshock/rest/models/device_response.py
  • openshock/rest/models/device_self_response.py
  • openshock/rest/models/device_with_shockers_response.py
  • openshock/rest/models/device_with_token_response.py
  • openshock/rest/models/duration_limit_settings.py
  • openshock/rest/models/given_control_stats.py
  • openshock/rest/models/hub_create_request.py
  • openshock/rest/models/hub_edit_request.py
  • openshock/rest/models/intensity_limit_settings.py
  • openshock/rest/models/lcg_node_response_v2.py
  • openshock/rest/models/lcg_response_v2.py
  • openshock/rest/models/legacy_data_response_of_array_of_device_response.py
  • openshock/rest/models/legacy_data_response_of_array_of_device_with_shockers_response.py
  • openshock/rest/models/legacy_data_response_of_array_of_log_entry.py
  • openshock/rest/models/legacy_data_response_of_array_of_own_public_share_response.py
  • openshock/rest/models/legacy_data_response_of_array_of_owner_shocker_response.py
  • openshock/rest/models/legacy_data_response_of_array_of_share_code_info.py
  • openshock/rest/models/legacy_data_response_of_array_of_share_info.py
  • openshock/rest/models/legacy_data_response_of_array_of_shocker_response.py
  • openshock/rest/models/legacy_data_response_of_backend_info_response.py
  • openshock/rest/models/legacy_data_response_of_device_self_response.py
  • openshock/rest/models/legacy_data_response_of_device_with_token_response.py
  • openshock/rest/models/legacy_data_response_of_guid.py
  • openshock/rest/models/legacy_data_response_of_legacy_user_self_response.py
  • openshock/rest/models/legacy_data_response_of_pause_reason.py
  • openshock/rest/models/legacy_data_response_of_public_share_response.py
  • openshock/rest/models/legacy_data_response_of_shocker_perm_limit_pair.py
  • openshock/rest/models/legacy_data_response_of_shocker_with_device.py
  • openshock/rest/models/legacy_data_response_of_stats_response.py
  • openshock/rest/models/legacy_data_response_ofboolean.py
  • openshock/rest/models/legacy_data_response_ofstring.py
  • openshock/rest/models/legacy_empty_response.py
  • openshock/rest/models/legacy_user_self_response.py
  • openshock/rest/models/log_entry.py
  • openshock/rest/models/log_entry_with_hub.py
  • openshock/rest/models/minimal_shocker.py
  • openshock/rest/models/new_shocker.py
  • openshock/rest/models/open_shock_problem.py
  • openshock/rest/models/own_public_share_response.py
  • openshock/rest/models/owner_shocker_response.py
  • openshock/rest/models/paged_result_of_log_entry_with_hub.py
  • openshock/rest/models/pause_request.py
  • openshock/rest/models/pause_user_share_shockers_request.py
  • openshock/rest/models/pause_user_share_shockers_response.py
  • openshock/rest/models/pause_user_share_shockers_response_pause_states.py
  • openshock/rest/models/permission_type.py
  • openshock/rest/models/problem_details.py
  • openshock/rest/models/public_share_author.py
  • openshock/rest/models/public_share_create.py
  • openshock/rest/models/public_share_device.py
  • openshock/rest/models/public_share_edit.py
  • openshock/rest/models/public_share_edit_shocker.py
  • openshock/rest/models/public_share_password_set.py
  • openshock/rest/models/public_share_response.py
  • openshock/rest/models/public_share_shocker.py
  • openshock/rest/models/public_share_unlock.py
  • openshock/rest/models/public_share_unlock_response.py
  • openshock/rest/models/public_user_profile_details.py
  • openshock/rest/models/public_user_profile_public_share.py
  • openshock/rest/models/public_user_profile_response.py
  • openshock/rest/models/received_control_stats.py
  • openshock/rest/models/remove_user_shares_response.py
  • openshock/rest/models/role_type.py
  • openshock/rest/models/share_code_info.py
  • openshock/rest/models/share_info.py
  • openshock/rest/models/share_invite_base_details.py
  • openshock/rest/models/shared_device.py
  • openshock/rest/models/shared_shocker.py
  • openshock/rest/models/shocker_control_settings.py
  • openshock/rest/models/shocker_control_stats.py
  • openshock/rest/models/shocker_limits.py
  • openshock/rest/models/shocker_model_type.py
  • openshock/rest/models/shocker_perm_limit_pair.py
  • openshock/rest/models/shocker_perm_limit_pair_with_id.py
  • openshock/rest/models/shocker_perm_limit_pair_with_id_and_name.py
  • openshock/rest/models/shocker_permissions.py
  • openshock/rest/models/shocker_response.py
  • openshock/rest/models/shocker_with_device.py
  • openshock/rest/models/social_platform.py
  • openshock/rest/models/sort_direction.py
  • openshock/rest/models/stats_response.py
  • openshock/rest/models/token_response_v2.py
  • openshock/rest/models/user_control_stat_totals.py
  • openshock/rest/models/user_control_stats.py
  • openshock/rest/models/user_profile_social.py
  • openshock/rest/models/user_self_response.py
  • openshock/rest/models/user_share_info.py
  • openshock/rest/models/v2_user_shares.py
  • openshock/rest/models/v2_user_shares_list_item.py
  • openshock/rest/types.py
  • pyproject.toml
  • scripts/generate.py
  • tests/fakes.py
  • tests/test_client.py
  • tests/test_generated.py
  • tests/test_live_control.py
  • tests/test_models.py
  • tests/test_user_hub.py

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@hhvrc

hhvrc commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

This branch depends on OpenShock/API#419 to drastically reduce amount of code generated

@hhvrc
hhvrc merged commit 73a54aa into master Dec 4, 2026
8 checks passed
@hhvrc
hhvrc deleted the feat/async-and-realtime branch December 4, 2026 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant